<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html lang="cn zh">
<head >
    <meta charset="UTF-8">
    <title>后台管理系统</title>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/admin.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-3.2.1.js"></script>
    <script>
        $(function () {
            $("span:eq(1)").click(function () {
                $("#content").html()
            })

            //图书信息管理
            $("span:eq(2)").click(function () {

                $.ajax({
                    type:"post",
                    url:"/login/bookServlet",
                    dataType:"json",
                    data:{
                        "doType":"doQuery",
                    },
                    success:function (data) {
                        var temp="<div id=\"book\">\n" +
                            "                    <table border=\"1\" style=\"border-collapse: collapse\">\n" +
                            "                        <caption>商品信息管理</caption>\n" +
                            "                        <thead>\n" +
                            "                        <tr>\n" +
                            "                            <th style=\"width: 150px\">图书ID</th>\n" +
                            "                            <th style=\"width: 200px\">书名</th>\n" +
                            "                            <th style=\"width: 200px\">作者</th>\n" +
                            "                            <th style=\"width: 200px\">库存</th>\n" +
                            "                            <th style=\"width: 200px\">价格</th>\n" +
                            "                            <th style=\"width: 100px\">删除</th>\n" +
                            "                            <th style=\"width: 100px\">变更</th>\n" +
                            "                            <th style=\"width: 100px\">查看详情</th>\n" +
                            "                        </tr>\n" +
                            "                        </thead>\n" +
                            "                        <tbody>\n"

                        for (var i=0;i<data.length;i++){
                            temp+="                        <tr style=\"text-align: center;height: 40px\">\n" +
                                "                            <td>"+data[i].bookId+"</td>\n" +
                                "                            <td>"+data[i].book+"</td>\n" +
                                "                            <td>"+data[i].writer+"</td>\n" +
                                "                            <td>"+data[i].repertory+"</td>\n" +
                                "                            <td>"+data[i].price+"</td>\n" +
                                "                            <td><span style=\"color: blue\" class='remove'>删除</span></td>\n" +
                                "                            <td><a href='/login/booksDetailsServlet?doType=doQuery&type=update&bookId="+data[i].bookId+"'target=\"_blank\">更改</a></td>\n" +
                                "                            <td><a href='/login/booksDetailsServlet?doType=doQuery&bookId="+data[i].bookId+"'target=\"_blank\">查看详情</a></td>\n" +
                                "                        </tr>\n"
                        }
                        temp+="                        </tbody>\n" +
                            "                    </table>\n" +
                            "                </div>"
                        $("#content").html(temp)

                        //对图书进行删除操作
                        $(".remove").each(function () {
                            $(this).click(function () {
                                var node=$(this).closest("td");
                                var node1=$(this).closest("tr");
                                var bookId=node.prev().prev().prev().prev().prev().html();
                                $.ajax({
                                    type:"post",
                                    url:"/login/booksDetailsServlet",
                                    data:{
                                        "doType":"doDelete",
                                        "bookId":bookId
                                    },
                                    success:function (data) {
                                        if(data==0){
                                            alert("删除失败！")
                                        }else {
                                            node1.remove();
                                        }
                                    }
                                })
                            })
                        })
                    }
                })


            })


            //添加图书分类
            $("span:eq(3)").click(function () {

                $.ajax({
                    type:"post",
                    url:"/login/titleServlet",
                    dataType:"json",
                    data:{
                        "doType":"doQuery",
                        "type":"titleOne"
                    },
                    success:function (data) {
                        var temp="<div id=\"addClass\">\n" +
                            "                   一级分类标题：\n" +
                            "                    <select>\n" +
                            "                        <option value=\"\">请选择标题----------</option>\n"
                        for (var i=0;i<data.length;i++){
                            temp+="<option value='"+data[i].id+"'>"+data[i].title+"</option>\n"
                        }
                        temp+="                    </select><br><br>\n" +
                            "                    二级分类标题：<input name='title'><br><br>\n" +
                            "                    &nbsp &nbsp  <input type=\"button\" value=\"提交\">\n" +
                            "                </div>"
                        $("#content").html(temp)


                        var titleOneId=$("select").val();
                        $("select").change(function () {
                            titleOneId=$("select").val();
                        })

                        $("[value=提交]").click(function () {
                            var title=$("[name=title]").val()
                            $.ajax({
                                type:"post",
                                url:"/login/titleServlet",
                                data:{
                                    "doType":"doInsert",
                                    "titleOneId":titleOneId,
                                    "title":title
                                },
                                success:function (data) {
                                    if(data==0){
                                        alert("添加失败！")
                                    }else {
                                        alert("添加成功！")
                                    }
                                }
                            })
                        })
                    }


                })
            })


            //管理图书分类
            $("span:eq(4)").click(function () {

                $.ajax({
                    type:"post",
                    url:"/login/titleServlet",
                    dataType:"json",
                    data:{
                        "doType":"doQuery"
                    },
                    success:function (data) {
                        var temp="<div id=\"adminClass\">\n" +
                            "                    <table border=\"1\" style=\"border-collapse: collapse\">\n" +
                            "                        <caption>分类信息</caption>\n" +
                            "                        <thead>\n" +
                            "                        <tr>\n" +
                            "                            <th style=\"width: 150px\">分类ID</th>\n" +
                            "                            <th style=\"width: 200px\">一级分类ID</th>\n" +
                            "                            <th style=\"width: 200px\">二级分类标题</th>\n" +
                            "                            <th style=\"width: 100px\">删除</th>\n" +
                            "                            <th style=\"width: 100px\">变更</th>\n" +
                            "                        </tr>\n" +
                            "                        </thead>\n" +
                            "                        <tbody>\n"

                        for (var i=0;i<data.length;i++){
                            temp+="                        <tr style=\"text-align: center;height: 40px\">\n" +
                                "                            <td>"+data[i].id+"</td>\n" +
                                "                            <td>"+data[i].titleOneId+"</td>\n" +
                                "                            <td><input value='"+data[i].title+"'></td>\n" +
                                "                            <td><span style=\"color: blue\" class='remove'>删除</span></td>\n" +
                                "                             <input type='hidden' value='"+data[i].id+"'>\n" +
                                "                            <td><span style=\"color: blue\" class='change'>更改</span></td>\n" +
                                "                        </tr>\n"
                        }
                        temp+="                        </tbody>\n" +
                            "                    </table>\n" +
                            "                </div>"
                        $("#content").html(temp)

                        //对二级分类标题进行删除操作
                        $(".remove").each(function () {
                            var node=$(this).closest("td");
                            var node1=$(this).closest("tr");
                            $(this).click(function () {
                                var id=node.next().val();
                                $.ajax({
                                    type:"post",
                                    url:"/login/titleServlet",
                                    data:{
                                        "doType":"doDelete",
                                        "id":id
                                    },
                                    success:function (data) {
                                        if(data==0){
                                            alert("删除失败！")
                                        }else {
                                            node1.remove();
                                        }
                                    }
                                })
                            })
                        })

                        //对二级分类标题进行变更操作
                        $(".change").each(function () {
                            $(this).click(function () {
                                var node=$(this).closest("td");
                                var id=node.prev().val();
                                var title=node.prev().prev().prev().children().val();
                                $.ajax({
                                    type:"post",
                                    url:"/login/titleServlet",
                                    data:{
                                        "doType":"doUpdate",
                                        "id":id,
                                        "title":title
                                    },
                                    success:function (data) {
                                        if(data==0){
                                            alert("更改失败！")
                                        }else {
                                            alert("更改成功！")
                                        }
                                    }
                                })
                            })
                        })
                    }
                })

            })


            //用户信息管理
            $("span:eq(5)").click(function () {
                $.ajax({
                    type:"post",
                    url:"/userServlet",
                    dataType:"json",
                    data:{
                        "doType":"doQuery",
                        "type":"all"
                    },
                    success:function (data) {
                        var temp="<div id=\"userMessage\">\n" +
                            "                    <table border=\"1\" style=\"border-collapse: collapse\">\n" +
                            "                        <caption>用户信息管理</caption>\n" +
                            "                        <thead>\n" +
                            "                        <tr>\n" +
                            "                            <th style=\"width: 150px\">用户ID</th>\n" +
                            "                            <th style=\"width: 200px\">用户Email</th>\n" +
                            "                            <th style=\"width: 200px\">用户昵称</th>\n" +
                            "                            <th style=\"width: 200px\">用户权限等级</th>\n" +
                            "                            <th style=\"width: 100px\">操作</th>\n" +
                            "                        </tr>\n" +
                            "                        </thead>\n" +
                            "                        <tbody>\n"

                        for(var i=0;i<data.length;i++){
                            temp+= "                        <tr style=\"text-align: center;height: 40px\">\n" +
                                "                            <td>"+data[i].id+"</td>\n" +
                                "                            <td>"+data[i].email+"</td>\n" +
                                "                            <td><input value='"+data[i].userName+"'></td>\n" +
                                "                            <td><input value='"+data[i].jurisdiction+"'></td>\n" +
                                "                            <td><span style=\"color: blue\" class='change'>更改</span></td>\n" +
                                "                        </tr>\n"
                        }
                        temp+="                        </tbody>\n" +
                            "                    </table>\n" +
                            "                </div>"

                        $("#content").html(temp)

                        $(".change").each(function () {
                            $(this).click(function () {
                                var node=$(this).closest("td");
                                var userId=node.prev().prev().prev().prev().html();
                                var userName=node.prev().prev().children().val();
                                var jurisdiction=node.prev().children().val();
                                $.ajax({
                                    type:"post",
                                    url:"/userServlet",
                                    data:{
                                        "doType":"doUpdate",
                                        "type":"admin",
                                        "userId":userId,
                                        "userName":userName,
                                        "jurisdiction":jurisdiction
                                    },
                                    success:function (data) {
                                        if(data==0){
                                            alert("更改失败！")
                                        }else {
                                            alert("更改成功！")
                                        }
                                    }
                                })
                            })
                        })
                    }
                })


            })

            //订单信息查看
            $("span:eq(6)").click(function () {
                $.ajax({
                    type:"post",
                    url:"/login/createOrderServlet",
                    dataType:"json",
                    data:{
                        "doType":"doQuery",
                        "type":"all"
                    },
                    success:function (data) {
                        var temp="<div id=\"order\" >\n" +
                            "                <table border=\"1\" style=\"border-collapse: collapse\">\n" +
                            "                    <caption><p>订单信息表</p></caption>\n" +
                            "                    <thead>\n" +
                            "                    <tr>\n" +
                            "                        <th>订单ID</th>\n" +
                            "                        <th>用户ID</th>\n" +
                            "                        <th>总价</th>\n" +
                            "                        <th>地址ID</th>\n" +
                            "                        <th>操作</th>\n" +
                            "                    </tr>\n" +
                            "                    </thead>\n" +
                            "                    <tbody>\n"

                        for(var i=0;i<data.length;i++){
                            temp+= "                    <tr style=\"height: 50px;text-align: center\">\n" +
                                "                        <td style=\"width: 200px;\">"+data[i].id+"</td>\n" +
                                "                        <td style=\"width: 200px;\">"+data[i].userId+"</td>\n" +
                                "                        <td style=\"width: 200px;\">"+data[i].price+"</td>\n" +
                                "                        <td style=\"width: 300px;\">"+data[i].addressId+"</td>\n" +
                                "                        <td style=\"width: 150px;color: blue\">" +
                                "<a href='/login/orderDetails?doType=doQuery&orderId="+data[i].id+"' target=\"_blank\">查看详情</a></td>\n" +
                                "                    </tr>\n"
                        }
                        temp+="</tbody>\n" +
                            "                </table>\n" +
                            "            </div>"
                        $("#content").html(temp);

                    }
                })
            })
        })
    </script>
</head>
<body>
    <div id="container"  align="center">
        <!--页面头部栏-->
        <div id="head">
            <p><span id="span1"><b>${userName}您好，欢迎光临当当网台管理系统！</b>[<a href="/userServlet?doType=doQuery&type=quit" target="-self">退出</a> ] </span></p>
              <!-- // <span  id="span2"><a href="login/cart.html" target="-self"><img src="images/gouwu.png">购物车</a> |<a href="">我的当当</a>|<a href="">帮助</a>| </span>-->
            <div id="picture">
                <a href="${pageContext.request.contextPath}/index.jsp" ><img src="${pageContext.request.contextPath}/images/dang.png"></a><h3>图书</h3>
            </div>
        </div>

        <!--注册信息栏-->
        <div id="center">
            <div id="menu">
                <div id="text">
                    菜单
                </div>
                <div class="content">
                    <h3>商品管理</h3>
                    <span><a href="booksDetailsAdmin.jsp" target="_blank" >添加商品</a></span><br>
                    <span>查看/修改商品</span>
                </div>
                <div class="content">
                    <h3>分类管理</h3>
                    <span>添加分类</span><br>
                    <span>查看/修改分类</span>
                </div>
                <div class="content">
                    <h3>用户管理</h3>
                    <span>查看/修改用户信息</span><br>
                </div>
                <div class="content">
                    <h3>订单管理</h3>
                    <span>查看订单</span><br>
                </div>
            </div>
            <div id="content">
                欢迎使用当当网后台管理系统！
            </div>
        </div>

        <!--页面脚注栏-->
        <div id="foot">
            <iframe src="${pageContext.request.contextPath}/foot.jsp"width="100%"height="100%"scrolling="no"frameborder="0">
            </iframe>
        </div>
    </div>
</body>
</html>
